import axios, { type AxiosResponse } from 'axios';
import { useUserStore } from "@/store";


const service = axios.create({
    baseURL: import.meta.env.VITE_GLOB_API_URL,
    timeout: !isNaN(+import.meta.env.VITE_GLOB_API_TIMEOUT) ? Number(import.meta.env.VITE_GLOB_API_TIMEOUT) : 60 * 1000,
})
const userStore = useUserStore();



service.interceptors.request.use(

    (config) => {
        config.headers['token'] = userStore.getAppointUserToken()
        config.headers['language'] = "1"
        return config
    },
    (error) => {
        return Promise.reject(error.response)
    },
)

service.interceptors.response.use(
    (response: AxiosResponse): AxiosResponse => {
        if (response.status === 200)
            return response
        throw new Error(response.status.toString())
    },
    (error) => {
        return Promise.reject(error)
    },
)

export default service
